Cartesian Coordinate System

A Cartesian coordinate system is made up of linear unit vectors perpendicular to each other. A particle's position is

$$\vec{r}=<x,y,z>$$

The unit vectors are:

$$\hat{x}=<1,0,0>$$$$\hat{y}=<0,1,0>$$$$\hat{z}=<0,0,1>$$

We can also write the position vector in terms of these unit vectors.

$$\vec{r}=x\hat{x}+y\hat{y}+z\hat{z}$$

The magnitude of the vector is

$$|\vec{r}|=\sqrt{x^2+y^2+z^2}$$

Suppose that the position vector makes angles with the x, y, z axes of $(\theta_x)$, $(\theta_y)$, $(\theta_z)$ respectively. You can calculate each angle using direction cosines.

$$\cos\theta_x = \frac{x}{|\vec{r}|}$$$$\cos\theta_y = \frac{y}{|\vec{r}|}$$$$\cos\theta_z = \frac{z}{|\vec{r}|}$$

Using iVisual to display a Cartesian Coordinate System

First, import the necessary packages.


In [2]:
from __future__ import division, print_function
from ivisual import *

Create the scene and draw axes and unit vectors. Unit vectors have a length of 1. The axes can be as long as we wish. In this case they are 15 units long.


In [8]:
#create scene
scene = canvas(title='Cartesian Coordinate System')

#instructions for manipulating the 3D scene
print("""
Right button drag to rotate "camera" to view scene.
  On a one-button mouse, right is Command + mouse.
Middle button to drag up or down to zoom in or out.
  On a two-button mouse, middle is left + right.
  On a one-button mouse, middle is Option + mouse.
""")

#draw x, y, z axes
L=15 #length of axes
sw=L/100 #shiftwidth of axes
xaxis=arrow(pos=(-L/2,0,0), axis=(L,0,0), shaftwidth=sw)
yaxis=arrow(pos=(0,-L/2,0), axis=(0,L,0), shaftwidth=sw)
zaxis=arrow(pos=(0,0,-L/2), axis=(0,0,L), shaftwidth=sw)

#draw unit vectors
xhat=arrow(pos=(0,0,0), axis=(1,0,0), shaftwidth=2*sw, color=color.orange)
yhat=arrow(pos=(0,0,0), axis=(0,1,0), shaftwidth=2*sw, color=color.orange)
zhat=arrow(pos=(0,0,0), axis=(0,0,1), shaftwidth=2*sw, color=color.orange)


Right button drag to rotate "camera" to view scene.
  On a one-button mouse, right is Command + mouse.
Middle button to drag up or down to zoom in or out.
  On a two-button mouse, middle is left + right.
  On a one-button mouse, middle is Option + mouse.

Defining and displaying a vector

Suppose that we want to display the vector $\vec{r}=<3,4,5>$ m. First define the vector in iVisual. Then draw an arrow with a tail at the origin and an axis equal to $\vec{r}$. Note that the resulting vector will be displayed in the scene shown above.


In [9]:
#define a vector
r=vector(3,4,5)

#draw the vector
rarrow=arrow(pos=(0,0,0), axis=r, shaftwidth=2*sw, color=color.yellow)

Since $\vec{r}=x\hat{x} + y\hat{y} + z\hat{z}$ then we can show that the sum of the components is equal to the vector. Let's draw the components. However, let's draw in a way that shows that their sum is equal to the vector $\vec{r}$. Once again, after executing the cell you will have to scroll up to view the scene. Rotate the scene around so that you can see the components.


In [11]:
#draw the vector components
rxarrow=arrow(pos=(0,0,0), axis=(r.x,0,0), shaftwidth=2*sw, color=color.red)
ryarrow=arrow(pos=rxarrow.axis, axis=(0,r.y,0), shaftwidth=2*sw, color=color.green)
rzarrow=arrow(pos=ryarrow.pos+ryarrow.axis, axis=(0,0,r.z), shaftwidth=2*sw, color=color.blue)

Calculating the angles between the vectors and the axes

The vector is $\vec{r}=<3,4,5>$ m. What is the magnitude of the vector and what angle does it make with each axis ($\theta_x,\, \theta_y,\, \theta_z$)? You should calculate it by hand first.

Now let's use iVisual to calculate it. The mag( ) function computes the magnitude of a vector.


In [12]:
#calculate the magnitude of the vector and print it
rmag=mag(r)
print("|r| = ", rmag)


|r| =  7.07106781187

In [20]:
#import math to get acos function
from math import *

#calculate the angle that the vector makes with each axis
#acos returns the angle in rad. You have to convert to deg if you want deg
theta_x = acos(r.x/mag(r))*180/pi
theta_y = acos(r.y/mag(r))*180/pi
theta_z = acos(r.z/mag(r))*180/pi

print("theta_x = ", theta_x)
print("theta_y = ", theta_y)
print("theta_z = ", theta_z)


theta_x =  64.8959097498
theta_y =  55.550098012
theta_z =  45.0

In [ ]: